// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;

internal sealed class GeoDistanceAggregationConverter : JsonConverter<GeoDistanceAggregation>
{
	public override GeoDistanceAggregation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		reader.Read();
		var aggName = reader.GetString();
		if (aggName != "geo_distance")
			throw new JsonException("Unexpected JSON detected.");
		var agg = new GeoDistanceAggregation(aggName);
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("distance_type"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType?>(ref reader, options);
					if (value is not null)
					{
						agg.DistanceType = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("field"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Field?>(ref reader, options);
					if (value is not null)
					{
						agg.Field = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("origin"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.GeoLocation?>(ref reader, options);
					if (value is not null)
					{
						agg.Origin = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("ranges"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>?>(ref reader, options);
					if (value is not null)
					{
						agg.Ranges = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("unit"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.DistanceUnit?>(ref reader, options);
					if (value is not null)
					{
						agg.Unit = value;
					}

					continue;
				}
			}
		}

		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("meta"))
				{
					var value = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
					if (value is not null)
					{
						agg.Meta = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("aggs") || reader.ValueTextEquals("aggregations"))
				{
					var value = JsonSerializer.Deserialize<AggregationDictionary>(ref reader, options);
					if (value is not null)
					{
						agg.Aggregations = value;
					}

					continue;
				}
			}
		}

		return agg;
	}

	public override void Write(Utf8JsonWriter writer, GeoDistanceAggregation value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("geo_distance");
		writer.WriteStartObject();
		if (value.DistanceType is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, value.DistanceType, options);
		}

		if (value.Field is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, value.Field, options);
		}

		if (value.Origin is not null)
		{
			writer.WritePropertyName("origin");
			JsonSerializer.Serialize(writer, value.Origin, options);
		}

		if (value.Ranges is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, value.Ranges, options);
		}

		if (value.Unit is not null)
		{
			writer.WritePropertyName("unit");
			JsonSerializer.Serialize(writer, value.Unit, options);
		}

		writer.WriteEndObject();
		if (value.Meta is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, value.Meta, options);
		}

		if (value.Aggregations is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, value.Aggregations, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(GeoDistanceAggregationConverter))]
public sealed partial class GeoDistanceAggregation : SearchAggregation
{
	public GeoDistanceAggregation(string name) => Name = name;

	internal GeoDistanceAggregation()
	{
	}

	public Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? Aggregations { get; set; }

	/// <summary>
	/// <para>The distance calculation type.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceType { get; set; }

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.Field? Field { get; set; }
	public IDictionary<string, object>? Meta { get; set; }
	override public string? Name { get; internal set; }

	/// <summary>
	/// <para>The origin  used to evaluate the distance.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.GeoLocation? Origin { get; set; }

	/// <summary>
	/// <para>An array of ranges used to bucket documents.</para>
	/// </summary>
	public ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>? Ranges { get; set; }

	/// <summary>
	/// <para>The distance unit.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.DistanceUnit? Unit { get; set; }
}

public sealed partial class GeoDistanceAggregationDescriptor<TDocument> : SerializableDescriptor<GeoDistanceAggregationDescriptor<TDocument>>
{
	internal GeoDistanceAggregationDescriptor(Action<GeoDistanceAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public GeoDistanceAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? AggregationsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument> AggregationsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>> AggregationsDescriptorAction { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceTypeValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoLocation? OriginValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>? RangesValue { get; set; }
	private AggregationRangeDescriptor RangesDescriptor { get; set; }
	private Action<AggregationRangeDescriptor> RangesDescriptorAction { get; set; }
	private Action<AggregationRangeDescriptor>[] RangesDescriptorActions { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.DistanceUnit? UnitValue { get; set; }

	public GeoDistanceAggregationDescriptor<TDocument> Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? aggregations)
	{
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = null;
		AggregationsValue = aggregations;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument> descriptor)
	{
		AggregationsValue = null;
		AggregationsDescriptorAction = null;
		AggregationsDescriptor = descriptor;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Aggregations(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>> configure)
	{
		AggregationsValue = null;
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>The distance calculation type.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> DistanceType(Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? distanceType)
	{
		DistanceTypeValue = distanceType;
		return Self;
	}

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	/// <summary>
	/// <para>The origin  used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> Origin(Elastic.Clients.Elasticsearch.Serverless.GeoLocation? origin)
	{
		OriginValue = origin;
		return Self;
	}

	/// <summary>
	/// <para>An array of ranges used to bucket documents.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> Ranges(ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>? ranges)
	{
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesValue = ranges;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Ranges(AggregationRangeDescriptor descriptor)
	{
		RangesValue = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesDescriptor = descriptor;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Ranges(Action<AggregationRangeDescriptor> configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorActions = null;
		RangesDescriptorAction = configure;
		return Self;
	}

	public GeoDistanceAggregationDescriptor<TDocument> Ranges(params Action<AggregationRangeDescriptor>[] configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>The distance unit.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor<TDocument> Unit(Elastic.Clients.Elasticsearch.Serverless.DistanceUnit? unit)
	{
		UnitValue = unit;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("geo_distance");
		writer.WriteStartObject();
		if (DistanceTypeValue is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, DistanceTypeValue, options);
		}

		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (OriginValue is not null)
		{
			writer.WritePropertyName("origin");
			JsonSerializer.Serialize(writer, OriginValue, options);
		}

		if (RangesDescriptor is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, RangesDescriptor, options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorAction is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new AggregationRangeDescriptor(RangesDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorActions is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			foreach (var action in RangesDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new AggregationRangeDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (RangesValue is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, RangesValue, options);
		}

		if (UnitValue is not null)
		{
			writer.WritePropertyName("unit");
			JsonSerializer.Serialize(writer, UnitValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (AggregationsDescriptor is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsDescriptor, options);
		}
		else if (AggregationsDescriptorAction is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, new AggregationDescriptor<TDocument>(AggregationsDescriptorAction), options);
		}
		else if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class GeoDistanceAggregationDescriptor : SerializableDescriptor<GeoDistanceAggregationDescriptor>
{
	internal GeoDistanceAggregationDescriptor(Action<GeoDistanceAggregationDescriptor> configure) => configure.Invoke(this);

	public GeoDistanceAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? AggregationsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor AggregationsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor> AggregationsDescriptorAction { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? DistanceTypeValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.GeoLocation? OriginValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>? RangesValue { get; set; }
	private AggregationRangeDescriptor RangesDescriptor { get; set; }
	private Action<AggregationRangeDescriptor> RangesDescriptorAction { get; set; }
	private Action<AggregationRangeDescriptor>[] RangesDescriptorActions { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.DistanceUnit? UnitValue { get; set; }

	public GeoDistanceAggregationDescriptor Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? aggregations)
	{
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = null;
		AggregationsValue = aggregations;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor descriptor)
	{
		AggregationsValue = null;
		AggregationsDescriptorAction = null;
		AggregationsDescriptor = descriptor;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Aggregations(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor> configure)
	{
		AggregationsValue = null;
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>The distance calculation type.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor DistanceType(Elastic.Clients.Elasticsearch.Serverless.GeoDistanceType? distanceType)
	{
		DistanceTypeValue = distanceType;
		return Self;
	}

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>A field of type `geo_point` used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	/// <summary>
	/// <para>The origin  used to evaluate the distance.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Origin(Elastic.Clients.Elasticsearch.Serverless.GeoLocation? origin)
	{
		OriginValue = origin;
		return Self;
	}

	/// <summary>
	/// <para>An array of ranges used to bucket documents.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Ranges(ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationRange>? ranges)
	{
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesValue = ranges;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Ranges(AggregationRangeDescriptor descriptor)
	{
		RangesValue = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesDescriptor = descriptor;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Ranges(Action<AggregationRangeDescriptor> configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorActions = null;
		RangesDescriptorAction = configure;
		return Self;
	}

	public GeoDistanceAggregationDescriptor Ranges(params Action<AggregationRangeDescriptor>[] configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>The distance unit.</para>
	/// </summary>
	public GeoDistanceAggregationDescriptor Unit(Elastic.Clients.Elasticsearch.Serverless.DistanceUnit? unit)
	{
		UnitValue = unit;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("geo_distance");
		writer.WriteStartObject();
		if (DistanceTypeValue is not null)
		{
			writer.WritePropertyName("distance_type");
			JsonSerializer.Serialize(writer, DistanceTypeValue, options);
		}

		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (OriginValue is not null)
		{
			writer.WritePropertyName("origin");
			JsonSerializer.Serialize(writer, OriginValue, options);
		}

		if (RangesDescriptor is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, RangesDescriptor, options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorAction is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new AggregationRangeDescriptor(RangesDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorActions is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			foreach (var action in RangesDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new AggregationRangeDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (RangesValue is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, RangesValue, options);
		}

		if (UnitValue is not null)
		{
			writer.WritePropertyName("unit");
			JsonSerializer.Serialize(writer, UnitValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (AggregationsDescriptor is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsDescriptor, options);
		}
		else if (AggregationsDescriptorAction is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, new AggregationDescriptor(AggregationsDescriptorAction), options);
		}
		else if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		writer.WriteEndObject();
	}
}